/**
 * -----------------------------------
 * Copyright (c) 2021-2024
 * All rights reserved, Designed By linfeng.tech , linfengtech.cn
 * 林风婚恋交友开源版本请务必保留此注释头信息
 * 开源地址: https://gitee.com/yuncoder001/linfeng-love.git
 * 商业版授权联系技术客服	 QQ:  973921677/3582996245
 * 严禁分享、盗用、转卖源码或非法牟利！
 * 版权所有 ，侵权必究！
 * -----------------------------------
 */
package io.linfeng.api.app.file.controller;

import io.linfeng.common.api.R;
import io.linfeng.service.file.service.FileService;
import io.linfeng.service.im.server.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * APP文件上传
 *
 */
@RestController
@RequestMapping("app/file")
@Api(tags = "文件上传Api")
public class AppFileController {

	@Resource
	private MinioService minioService;


	private final FileService fileService;

	public AppFileController(FileService fileService) {
		this.fileService = fileService;
	}

	/**
	 * 文件上传
	 * @param file 待上传文件
	 * @return 文件url
	 */
	@ApiOperation("上传文件")
	@PostMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile file) {
		String url = fileService.uploadMinio(file);
		return R.ok().put("result",url);
	}


	@PostMapping("/uploadMinio")
	public Object uploadMinio(@RequestParam("file") MultipartFile multipartFile) throws Exception {
		return this.minioService.putObject(multipartFile);
	}

	// 下载文件
	@GetMapping("/download")
	public void download(@RequestParam("fileName")String fileName, HttpServletResponse response) {
		this.minioService.download(fileName,response);
	}

	// 列出所有存储桶名称
	@PostMapping("/list")
	public List<String> list() throws Exception {
		return this.minioService.listBucketNames();
	}

	// 创建存储桶
	@PostMapping("/createBucket")
	public boolean createBucket(String bucketName) throws Exception {
		return this.minioService.makeBucket(bucketName);
	}

	// 删除存储桶
	@PostMapping("/deleteBucket")
	public boolean deleteBucket(String bucketName) throws Exception {
		return this.minioService.removeBucket(bucketName);
	}

	// 列出存储桶中的所有对象名称
	@PostMapping("/listObjectNames")
	public List<String> listObjectNames(String bucketName) throws Exception {
		return this.minioService.listObjectNames(bucketName);
	}

	// 删除一个对象
	@PostMapping("/removeObject")
	public boolean removeObject(String bucketName, String objectName) throws Exception {
		return this.minioService.removeObject(bucketName, objectName);
	}

	// 文件访问路径
	@PostMapping("/getObjectUrl")
	public String getObjectUrl(String bucketName, String objectName) throws Exception {
		return this.minioService.getObjectUrl(bucketName, objectName);
	}

}
